<?php

defined('SYSPATH') or die('Access error!');

/**
 *  用户的经验处理类
 */
class Model_User_Experience extends Model_User_Account
{
   
    /**
     * 获取经验的配置
     */
    public function get_experience_config()
    {
        return $this->get_account_config(USER_ACCOUNT_EXPERIENCE);
    }

    /**
     * 获取经验的系统设置
     * @return type
     */
    public function get_experience_setting()
    {
        return $this->get_account_setting(42);
    }

    /**
     * 处理经验的配置和系统的设置
     * @param type $code
     * @return type
     */
    public function handle_experience_config_setting($code)
    {
        $this->account_config = $this->get_experience_config();
        $this->account_setting = $this->get_experience_setting();
        return $this->handle_account_config_setting($code);
    }

    /**
     * 根据用户的id获得某用户的当前经验
     * @param type $user_id
     * @return type
     */
    public function get_experience($user_id)
    {
        $user_points = $this->get_user_account($user_id, USER_ACCOUNT_EXPERIENCE);
        return $user_points[USER_ACCOUNT_EXPERIENCE];
    }

    /**
     *  保存经验和日志
     * @param type $to_user_id
     * @param type $code
     * @param type $detail_id
     * @param type $from_user_id
     * @return boolean
     */
    public function save_experience($to_user_id, $code, $detail_id, $from_user_id = 0)
    {
        $this->to_user_id = $to_user_id;
        //根据id获取用户当前的积分
        $this->current_account = $this->get_experience($to_user_id);
        $this->from_user_id = $from_user_id;
        $points_config = $this->handle_experience_config_setting($code);
        $this->message = $points_config['message'];
        $this->account = $points_config['account'];
        $this->time = $points_config['time'];
        $this->operation = $points_config['operation'];
        $this->type = USER_ACCOUNT_EXPERIENCE;
        $this->detail_id = $detail_id;
        //更新用户的积分
        if ($this->update_account())
        {
            //更新用户日志
            $this->update_account_log();
            return TRUE;
        }
    }

}